﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HidesaburoDotNET.Core2
{
    public enum Kaze
    {
        Ton,
        Nan,
        Sha,
        Pe
    }
    public enum Call
    {
        None = 0,
        DoNothing,
        Chi,
        Pon,
        Kan,
        Ron
    }
    public class Hai
    {
    }
    public class Yama : IYama
    {
        public Hai Draw() { return new Hai(); }

        #region IYama メンバ

        public int Left
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
    public class Hou : IHou
    {
        #region IHou メンバ

        public List<Hai> Discards
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        internal void Add(Hai hai)
        {
            throw new NotImplementedException();
        }
        public Hai LastDiscard { get { return new Hai(); } }
    }
    class RingArray
    {
        int offset = 0;
        Player[] players;
        public int Length { get { return players.Length; } }

        public Player this[int index]
        {
            get { return players[(index + offset) % players.Length]; }
        }

        public RingArray(Player[] players)
        {
            this.players = players;
        }

        public void Increament()
        {
            offset = (offset + 1) % players.Length;
        }
        public void Select(Player player)
        {
            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] == player)
                {
                    offset = i;
                    return;
                }
            }
        }
        public int GetInitiative(Player player)
        {
            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] == player)
                {

                    return (i + players.Length - offset) % players.Length;
                }
            }
            return 1000;
        }

    }

    public class Field : IFieldInfo
    {

        BreakEventArgs lastBreakEventArgs;
        public BreakEventArgs LastBreakEventArgs
        {
            get { return lastBreakEventArgs; }
        }

        List<Player> players;
        RingArray playerRing;

        Yama yama;

        public void GameStart()
        {
        }

        private void TriggerTsumo()
        {
            playerRing[0].Tsumo(yama.Draw());
        }

        internal void OnDiscarded(Player player,Hai discard)
        {
            if (player != playerRing[0])
            {
                new Exception("エラー");
            }

            calls = new Call[players.Count];

            for (int i = 1; i < playerRing.Length; i++)
            {
                playerRing[i].EnemyDiscarded(player, discard);
            }

            
        }
        Call[] calls;
        internal void Called(Player player, Call call)
        {
            calls[playerRing.GetInitiative(player)] = call;
            if (EveryoneDecleared())
            {
                if (!CheckRon())
                {
                }
                else if (!CheckPon())
                {
                }
                else if (!ChechChi())
                {
                }
                else
                {
                    playerRing.Increament();
                }
            }
        }

        private bool ChechChi()
        {
            throw new NotImplementedException();
        }

        private bool CheckPon()
        {
            throw new NotImplementedException();
        }

        private bool CheckRon()
        {
            if (this.Rule.PermitDoubleRon)
            {
                return false;
            }
            else
            {
                for (int i = 1; i < calls.Length; i++)
                {
                    if (calls[i] == Call.Ron)
                    {
                        EndByRon(playerRing[i], playerRing[0]);
                        return true;
                    }
                }
                return false;
            }
        }

        bool EveryoneDecleared()
        {
            for (int i = 1; i < calls.Length; i++)
            {
                if (calls[i] == Call.None) return false;
            }
            return true;
        }


        private void EndByRon(Player winner, Player target)
        {
        }




        #region IFieldInfo メンバ

        public int Kyoku
        {
            get { throw new NotImplementedException(); }
        }

        public int Renchan
        {
            get { throw new NotImplementedException(); }
        }

        public GameRule Rule
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }

    public delegate void RestartDelegate(Player player);

    public class BreakEventArgs : EventArgs
    {
        bool restartImmediately = false;
        public bool RestartImmediately
        {
            get { return restartImmediately; }
            set { restartImmediately = value; }
        }
    }

    public delegate void BreakEventHandler(Player sender,BreakEventArgs args);

}
